package com.activenetwork.util;

import java.util.Random;

public class RandomString {
	
	/**
	 * Create random string
	 */
	private static Random randGen = null;
	private static char[] numbersAndLetters = null;
	private static Object initLock = new Object();


	public static final String generateRandomString(int length) {

	         if (length < 1) {
	             return null;
	         }
	         
	         if(numbersAndLetters == null){
	        	 synchronized(initLock){
	        		 numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); 
	        	 }
	         }
	        	     
	         if (randGen == null) {
	             synchronized (initLock) {
	            	 randGen = new Random();
	             }
	         }
	         
	         char [] randBuffer = new char[length];
	         for (int i=0; i<randBuffer.length; i++) {
	             randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
	         }
	         return new String(randBuffer);
	}
	
	public static final String generateReplicateString(int times, String s){
		if(times < 1 || s.length() == 0){
			return null;
		}
		StringBuilder builder = new StringBuilder();
		
		synchronized (initLock) {
		
			for(int i=0;i<times;i++){
				builder.append(s);
			}
        }
		
		return builder.toString();
		
	}
	
	public static final int generateRandomInt(int max){
		int digit = -1;
		if(max < 0){
			return digit;
		}
		if(randGen == null){
			synchronized (initLock) {
				randGen = new Random();
			}
		}
			
		digit = randGen.nextInt(max);
		
		return digit;
	}
	

}
